package com.example.eback.controller;

import com.example.eback.dao.FilesDAO;
import com.example.eback.dao.OrganInfoDAO;
import com.example.eback.entity.OrganInfo;
import com.example.eback.entity.UpLoadFile;
import com.example.eback.entity.User;
import com.example.eback.result.MeasurementData;
import com.example.eback.result.Result;
import com.example.eback.result.ResultCode;
import com.example.eback.result.ResultFactory;
import com.example.eback.service.AnalyzeService;
import com.example.eback.service.FileProcessService;
import com.example.eback.service.OrganInfoService;
import com.example.eback.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
@RestController
public class AnalyzeController {
    @Autowired
    private OrganInfoService organInfoService;
    @Autowired
    private FilesDAO filesDAO;
    @Autowired
    private UserUtils userUtils;
    @Autowired
    private OrganInfoDAO organInfoDAO;
    @Autowired
    private FileProcessService fileProcessService;
    @Autowired
    private AnalyzeService analyzeService;

    /**
     * 数据分析，分析器官的各种信息，信息内容可见entity.OrganInfo
     */
    @PostMapping("/measurement")
    public Result LabelMeasurement(@RequestBody Map<String,Object> json) {
        try {
            //传入flask后端进行数据分析，返回一个 MeasurementData 对象，里面包含了器官的信息
            User current_User=userUtils.getCurrentUser();
            int current_userID=current_User.getId();
            int historyid=(int)json.get("historyid");
            analyzeService.Measurement(current_userID,historyid);
            List<OrganInfo> organInfoList=analyzeService.getOrganInfo(historyid);
            if(organInfoList!=null){
                return ResultFactory.buildResult(ResultCode.SUCCESS,"数据分析成功",organInfoList);
            }
            else{
                return ResultFactory.buildResult(ResultCode.FAIL,"数据分析失败",organInfoList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultFactory.buildFailResult(e.getMessage());
        }
    }
    @PostMapping("/calculateDistance")
    public Result CalulateDistance(@RequestBody Map<String,Object> json) {
        try {
            //传入flask后端进行数据分析，返回一个 数字 对象
            User current_User=userUtils.getCurrentUser();
            int current_userID=current_User.getId();
            int historyid=(int)json.get("historyid");
            int label_one=(int)json.get("Label_one");
            int label_two =(int)json.get("Label_two");
            double distance=analyzeService.CalculateDistance(current_userID,historyid,label_one,label_two);

            if(distance!=0){
                return ResultFactory.buildResult(ResultCode.SUCCESS,"两器官距离计算成功",distance);
            }
            else{
                return ResultFactory.buildResult(ResultCode.FAIL,"计算失败",null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultFactory.buildFailResult(e.getMessage());
        }
    }


}
